﻿using Application.Service.Contract;
using Solar.Core;
using Application.Domain;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Web;
using System.Configuration;
using System.IO;
using Solar.Infrastructure;
using Solar.Security;

namespace Application.Service
{
    public class AdminService
    {
        public const string AuthenticationType = "Application";

       
        public IUserPrincipal AuthenticateStaff(string username, string password)
        {
            using (var repository = DomainRepository.Open())
            {
                var account = repository.GetOne<Account>(c => c.Username == username);

                if (account != null && account.Password.Equals(password))
                {
                    return new UserPrincipal(new UserIdentity(account.Id, account.Username, account.Username, AuthenticationType, true), this.GetRoles(account));
                }

                return UserPrincipal.Unidentified;
            }
        }

        private IEnumerable<string> GetRoles(Account account)
        {
            var roles = new List<string>();

            //foreach (var accountRole in account.Roles)
            //{
            //    roles.Add(string.Format("{0}|{1}", accountRole.Department.Name, accountRole.Role.Name));
            //}

            return roles;
        }
        
    }
}

